package net.lovesrc.support.service.mobileverifycode;

import com.alibaba.fastjson.JSONObject;
import com.zhouzhou.fastjava.BizException;
import com.zhouzhou.fastjava.http.Requests;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class XiaoBaiMobileVerifyCodeService extends AbstractMobileVerifyCodeService implements MobileVerifyCodeService {

    private static final Logger logger = LoggerFactory.getLogger(XiaoBaiMobileVerifyCodeService.class);

    public static final String DEFAULT_HOST = "http://api.xiaobai188.com:188";

    private String username;

    private String password;

    private String gatewayUrl;

    public XiaoBaiMobileVerifyCodeService(String host, String username, String password) {
        this.username = username;
        this.password = password;
        this.gatewayUrl = host + "/api";
    }

    @Override
    protected String doGetVerifyCodeFromPhone(String itemId, String phone) {
        JSONObject params = new JSONObject();
        params.put("sid", itemId);
        params.put("token", getToken());
        params.put("phone", phone);
        JSONObject result = Requests.get(gatewayUrl + "/getMsg")
                .params(params)
                .execute()
                .asJson();
        if ("获取验证码成功~".equals(result.getString("msg"))) {
            return result.getJSONObject("data").getString("code");
        }
        return null;
    }

    @Override
    public void addBlack(String itemId, String phone) {
        // http://api.xiaobai188.com:188/api/blacklist?token=token&sid=项目ID&phone=已获取手机号
        JSONObject params = new JSONObject();
        params.put("sid", itemId);
        params.put("token", getToken());
        params.put("phone", phone);
       Requests.get(gatewayUrl+"/blacklist")
               .params(params)
               .execute()
               .asText();
    }

    @Override
    public void releasePhone(String itemId, String phone) {
        // http://api.xiaobai188.com:188/api/ReleasePhone?token=token&sid=项目ID&phone=已获取号码
        JSONObject params = new JSONObject();
        params.put("sid", itemId);
        params.put("token", getToken());
        params.put("phone", phone);
        Requests.get(gatewayUrl+"/ReleasePhone")
                .params(params)
                .execute()
                .asText();
    }


    /**
     *
     token
     登录返回的Token
     sid
     项目ID
     num
     取号数量默认空为1
     type
     0/空不限 1实卡 2虚拟运营商
     pr
     广东（省份不带省字）
     ci
     广州（市不带市字）
     sr
     服务商 0 / 空 不限 1中国电信 2中国联通 3中国移动
     phone
     指定的手机号
     kaifa
     开发者账号（登录账号） 填入开发者参数将获得平台利润的50%
     haoduan
     指定的号段：138,150 用英文逗号分隔多个号段
     */
    @Override
    public String getPhone(String itemId, String phone, String card) {
        JSONObject params = new JSONObject();
        params.put("sid", itemId);
        params.put("token", getToken());
        params.put("type", card);
        params.put("kaifa", "zxl8899");
        if (StringUtils.isNotEmpty(phone)){
            params.put("phone", phone);
        }
        JSONObject result = Requests.get(gatewayUrl + "/getPhone")
                .params(params)
                .execute()
                .asJson();
        if ("Success".equals(result.getString("msg"))) {
            return ((JSONObject)result.getJSONArray("data").get(0)).getString("phone");
        } else {
            throw BizException.messageOf("获取手机号失败");
        }
    }

    @Override
    protected String login() {
        JSONObject body = Requests.get(gatewayUrl + "/login")
                .param("password", password)
                .param("apiuser", username)
                .param("isup","0")
                .execute()
                .asJson();
        if ("20000".equals(body.getString("code"))) {
            return body.getJSONObject("data").getString("token");
        } else {
            throw BizException.messageOf("登录失败");
        }
    }
    //http://api.xiaobai188.com:188/api/login?apiuser=API账号&password=登录密码
}
